# No operating system
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.10)

# specify cross compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(CMAKE_SIZE arm-none-eabi-size)
# skip compiler checks
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# 设置 C 和 C++ 源文件编译后的目标文件扩展名为 .o
set(CMAKE_C_OUTPUT_EXTENSION .o)
set(CMAKE_CXX_OUTPUT_EXTENSION .o)

# 设置项目名称和语言
project(stepmotor C ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

# 编译选项
add_compile_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork -mfloat-abi=soft)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)

# RT-Thread源文件
file(GLOB RT_THREAD_SRC
    "${CMAKE_SOURCE_DIR}/../../../rt-thread/src/*.c"
    "${CMAKE_SOURCE_DIR}/../../../rt-thread/libcpu/arm/cortex-m0/*.c"
    "${CMAKE_SOURCE_DIR}/../../../rt-thread/libcpu/arm/cortex-m0/context_gcc.S"
    "${CMAKE_SOURCE_DIR}/../../../rt-thread/components/finsh/*.c"
)

# 排除finsh_port.c
list(REMOVE_ITEM RT_THREAD_SRC "${CMAKE_SOURCE_DIR}/../../../rt-thread/components/finsh/finsh_port.c")

# N32G031驱动库源文件
file(GLOB N32G031_SRC
    "${CMAKE_SOURCE_DIR}/../../../libraries/n32g031_std_periph_driver/src/*.c"
    "${CMAKE_SOURCE_DIR}/../../../libraries/CMSIS/device/system_n32g031.c"
    "${CMAKE_SOURCE_DIR}/../../../libraries/CMSIS/device/startup/startup_n32g031_gcc.s"
    "${CMAKE_SOURCE_DIR}/../../../drivers/*.c"
)

# 项目源文件
set(PROJECT_SRC
    ${CMAKE_SOURCE_DIR}/../main.c
    ${CMAKE_SOURCE_DIR}/../../../RTE/RTOS/board.c
)

# 包含目录
include_directories(
    ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/../../../rt-thread/include
    ${CMAKE_SOURCE_DIR}/../../../rt-thread/components/finsh
    ${CMAKE_SOURCE_DIR}/../../../libraries/CMSIS/core
    ${CMAKE_SOURCE_DIR}/../../../libraries/CMSIS/device
    ${CMAKE_SOURCE_DIR}/../../../libraries/n32g031_std_periph_driver/inc
    ${CMAKE_SOURCE_DIR}/../../../drivers/inc
    ${CMAKE_SOURCE_DIR}/../../../RTE/RTOS
    ${CMAKE_SOURCE_DIR}/..
)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/../gcc_n32g031.ld)

add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m0 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})

# 定义
add_definitions(
    -DN32G031
    -DUSE_STDPERIPH_DRIVER
    -DUSE_FULL_ASSERT
)

# 构建目标
add_executable(${PROJECT_NAME}.elf ${RT_THREAD_SRC} ${N32G031_SRC} ${PROJECT_SRC} ${LINKER_SCRIPT})

# 生成二进制文件
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
    COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
    COMMAND ${CMAKE_SIZE} ${PROJECT_NAME}.elf
    COMMENT "Building ${HEX_FILE} Building ${BIN_FILE}"
)
